package twitter.client.task;

import twitter.client.util.Utils;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class TweetTask extends AsyncTask<Twitter, Void, Void> {
	
	private Activity context;
	private ProgressDialog progress;
	private EditText postText;
	
	public TweetTask(Activity context, EditText postText){
		this.context = context;
		this.postText = postText;
	}

	@Override
	protected void onPreExecute() {
		progress = new ProgressDialog(this.context);
		progress.setMessage("送信中...");
		progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progress.show();
	}	
	
	@Override
	protected Void doInBackground(Twitter... params) {
		Twitter twitter = params[0];
		 
		String text = postText.getText().toString();
		try{
			twitter.updateStatus(text);
			
			InputMethodManager imm = (InputMethodManager) this.context.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(postText.getWindowToken(), 0);
			
		}catch(TwitterException e){
			Utils.showToast(this.context, e.getErrorMessage());
		}
		
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		postText.setText("");
		progress.dismiss();
	}
}
